package com.idp.web.test.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.common.base.BaseController;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;
import com.idp.web.system.entity.SysOrg;
import com.idp.web.test.dao.LogisticsDao;
import com.idp.web.test.entity.Logistics;
import com.idp.web.test.service.LogisticsService;

import net.sf.json.JSONObject;

/**
 * 
 * 123213132controller
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-02 Amber
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 *         <pre>
 * SD
 * 	Amber
 * PG
 *	Amber
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Controller
@RequestMapping("/logistics")
public class LogisticsController extends BaseController {

	private Logger logger = Logger.getLogger(LogisticsController.class);

	@Resource
	private LogisticsService logisticsService;

	@Resource
	private LogisticsDao logisticsDao;

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	初始化查询页面
	 * </pre>
	 * 
	 * @return
	 */
	@RequestMapping("/init")
	public String init() {

		return "test/logisticsSearch";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	分页查询
	 * </pre>
	 * 
	 * @param logistics
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(HttpServletRequest request) {

		try {

			List<Logistics> loglist = logisticsService.findBySearch(null);
			System.err.println(loglist == null ? "null" : loglist.size());
			for (Logistics logistics : loglist) {
				System.err.println(logistics.getName());
			}
			request.setAttribute("loglist", loglist);
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
		}

		return "test/logisticsList";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	新增修改页面初始化
	 * </pre>
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/logistics")
	public String logistics(Integer id, HttpServletRequest request) {

		try {

			if (ValidateUtils.isNotEmpty(id)) {

				Logistics logistics = logisticsService.getById(id);
				request.setAttribute("logistics", logistics);
			}
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
		}

		return "test/logistics";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	保存
	 * </pre>
	 * 
	 * @param logistics
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(Logistics logistics) {

		System.err.println(logistics.getName());

		String type = logistics.getType();

		if (type != null) {

			if (type.equals("true")) {
				logistics.setType("1");
			}

			if (type.equals("false")) {
				logistics.setType("0");
			}

		}

		JSONObject json = new JSONObject();

		try {

			// 修改
			if (ValidateUtils.isNotEmpty(logistics.getId())) {
				logisticsDao.updateNotNull(logistics);
			}
			// 新增
			else {

				logisticsService.add(logistics);
			}
			json.put("result", "保存成功");
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
			json.put("result", "保存失败");
		}

		return json.toString();
	}

	@RequestMapping("/addShip")
	@ResponseBody
	public String addShip(HttpServletRequest request) {

		String shipname = request.getParameter("shipname");
		String shipcode = request.getParameter("shipcode");
		JSONObject json = new JSONObject();

		Logistics logistics = logisticsDao.getIdByName(shipname);

		try {

			// 修改
			if (logistics != null) {

				logistics.setName(shipname);
				logistics.setCode(shipcode);
				logistics.setDescription(shipcode);
				logistics.setTrackingNumber(logistics.getId() + "");
				logistics.setType("1");
				logisticsDao.updateNotNull(logistics);
			}
			// 新增
			else {
				logistics = new Logistics();
				logistics.setName(shipname);
				logistics.setCode(shipcode);
				logistics.setDescription(shipcode);
				logistics.setType("1");
				logisticsService.add(logistics);
			}

			json.put("result", "save_success");

		} catch (Exception e) {

			logger.error(e.getMessage(), e);
			json.put("result", "save_fail");
		}

		return json.toString();
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	删除
	 * </pre>
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Integer id) {

		JSONObject json = new JSONObject();

		try {

			logisticsService.delete(id);
			json.put("result", "delete_success");
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
			json.put("result", "delete_fail");
		}

		return json.toString();
	}
}
